package com.ruoyi.labdanger.controller;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.labdanger.domain.LabApplyOccupationDeliver;
import com.ruoyi.labdanger.domain.LabApplyOccupationDetails;
import com.ruoyi.labdanger.service.ILabApplyOccupationDeliverService;
import com.ruoyi.labdanger.service.ILabApplyOccupationDetailsService;

/**
 * 申请领用明细Controller
 * 
 * @author ruoyi
 * @date 2022-03-09
 */
@RestController
@RequestMapping("/labdanger/details")
public class LabApplyOccupationDetailsController extends BaseController {
	@Autowired
	private ILabApplyOccupationDetailsService labApplyOccupationDetailsService;

	@Autowired
	private ILabApplyOccupationDeliverService labApplyOccupationDeliverService;

	/**
	 * 查询申请领用明细列表
	 */
	//@PreAuthorize("@ss.hasPermi('labdanger:details:query')")
	@GetMapping("/list")
	public TableDataInfo list(LabApplyOccupationDetails labApplyOccupationDetails) {
		startPage();
		List<LabApplyOccupationDetails> list = labApplyOccupationDetailsService
				.selectLabApplyOccupationDetailsList(labApplyOccupationDetails);
		return getDataTable(list);
	}
	
	/**
	 * 查询申请领用明细列表
	 */
	@PreAuthorize("@ss.hasPermi('labdanger:scanout:query')")
	@GetMapping("/query")
	public TableDataInfo query(LabApplyOccupationDetails labApplyOccupationDetails) {
		startPage();
		List<LabApplyOccupationDetails> list = labApplyOccupationDetailsService
				.queryLabApplyOccupationDetailsList(labApplyOccupationDetails);
		return getDataTable(list);
	}

	/**
	 * 导出申请领用明细列表
	 */
	// @PreAuthorize("@ss.hasPermi('labdanger:details:export')")
	/*
	 * @Log(title = "申请领用明细", businessType = BusinessType.EXPORT)
	 * 
	 * @GetMapping("/export") public AjaxResult export(LabApplyOccupationDetails
	 * labApplyOccupationDetails) { List<LabApplyOccupationDetails> list =
	 * labApplyOccupationDetailsService.selectLabApplyOccupationDetailsList(
	 * labApplyOccupationDetails); ExcelUtil<LabApplyOccupationDetails> util =
	 * new
	 * ExcelUtil<LabApplyOccupationDetails>(LabApplyOccupationDetails.class);
	 * return util.exportExcel(list, "details"); }
	 */
	/**
	 * 获取申请领用明细详细信息
	 */
	@PreAuthorize("@ss.hasPermi('labdanger:details:query')")
	@GetMapping(value = "/{occupationDetailsId}")
	public AjaxResult getInfo(@PathVariable("occupationDetailsId") Long occupationDetailsId) {
		LabApplyOccupationDetails applyOccupationDetails = labApplyOccupationDetailsService
				.selectLabApplyOccupationDetailsById(occupationDetailsId);
		LabApplyOccupationDeliver query = new LabApplyOccupationDeliver();
		query.setOccupationDetailsId(applyOccupationDetails.getOccupationDetailsId());
		List<LabApplyOccupationDeliver> deliverList = labApplyOccupationDeliverService
				.selectLabApplyOccupationDeliverList(query);
		applyOccupationDetails.setDeliverList(deliverList);
		return AjaxResult.success(applyOccupationDetails);
	}

	/**
	 * 新增申请领用明细
	 */
	@PreAuthorize("@ss.hasPermi('labdanger:details:edit')")
	@Log(title = "申请领用明细", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody LabApplyOccupationDetails labApplyOccupationDetails) {
		LabApplyOccupationDetails query = new LabApplyOccupationDetails();
		query.setInstanceId(labApplyOccupationDetails.getInstanceId());
		query.setPurchaseDetailId(labApplyOccupationDetails.getPurchaseDetailId());
		List<LabApplyOccupationDetails> list = labApplyOccupationDetailsService
				.selectLabApplyOccupationDetailsList(query);
		if (CollectionUtils.isNotEmpty(list)) {
			return AjaxResult.error("该试剂已经存在");
		}
		return toAjax(labApplyOccupationDetailsService.insertLabApplyOccupationDetails(labApplyOccupationDetails));
	}

	/**
	 * 修改申请领用明细
	 */
	@PreAuthorize("@ss.hasPermi('labdanger:details:edit')")
	@Log(title = "申请领用明细", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody LabApplyOccupationDetails labApplyOccupationDetails) {
		return toAjax(labApplyOccupationDetailsService.updateLabApplyOccupationDetails(labApplyOccupationDetails));
	}

	/**
	 * 删除申请领用明细
	 */
	@PreAuthorize("@ss.hasPermi('labdanger:details:edit')")
	@Log(title = "申请领用明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{occupationDetailsId}")
	public AjaxResult remove(@PathVariable Long occupationDetailsId) {
		return toAjax(labApplyOccupationDetailsService.deleteLabApplyOccupationDetailsById(occupationDetailsId));
	}

	@PreAuthorize("@ss.hasPermi('labdanger:scanout:edit')")
	@Log(title = "申请领用明细", businessType = BusinessType.UPDATE)
	@PostMapping("/deliver")
	public AjaxResult deliver(@RequestBody LabApplyOccupationDetails labApplyOccupationDetails) {
		return toAjax(labApplyOccupationDetailsService.deliver(labApplyOccupationDetails));
	}
}
